﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Workbench.Models;
using Workbench.Operations;
using Workbench.Persistance;

namespace WorkbenchTests
{
[TestClass]
public class ConcordancerOperationsTests
{
    [TestInitialize]
    public void Setup()
    {
        TestUtility.CreateAndInitializeTempDatabase();
    }

    [TestCleanup]
    public void Teardown()
    {
        DBContext.Current.UnInitialize();
    }

    [TestMethod]
    public void ConcordancerOperationsTests_GetAdjacentWords()
    {
        var file = TestUtility.CreateProcessedFile("This is a simple text file.");
        var words = ConcordanceOperations.GetSuggestions("a", SuggestionType.WordForm);
        Assert.AreEqual(1, words.Count);
        var aWord = words[0];
        Assert.AreEqual("a", aWord.Content);

        var nearWords = ConcordanceOperations.GetNearWords(aWord, SuggestionType.WordForm);

        Assert.AreEqual("is", nearWords[0].WordsToTheLeft[0]);
        Assert.AreEqual("This", nearWords[0].WordsToTheLeft[1]);

        Assert.AreEqual("simple", nearWords[0].WordsToTheRight[0]);
        Assert.AreEqual("text", nearWords[0].WordsToTheRight[1]);
        Assert.AreEqual("file", nearWords[0].WordsToTheRight[2]);

        Assert.AreEqual("a", nearWords[0].Word);

        TestUtility.LemmatizeWordsSameAsOriginalWordWithoutLastCharacter();

        var wordsLemma = ConcordanceOperations.GetSuggestions("simpl", SuggestionType.BaseForm);
        var nearWordsLemma = ConcordanceOperations.GetNearWords(wordsLemma[0], SuggestionType.BaseForm);

        Assert.AreEqual("a", nearWordsLemma[0].WordsToTheLeft[0]);
        Assert.AreEqual("is", nearWordsLemma[0].WordsToTheLeft[1]);
        Assert.AreEqual("This", nearWordsLemma[0].WordsToTheLeft[2]);

        Assert.AreEqual("text", nearWordsLemma[0].WordsToTheRight[0]);
        Assert.AreEqual("file", nearWordsLemma[0].WordsToTheRight[1]);

        Assert.AreEqual("simple", nearWordsLemma[0].Word);
    }
     

    [TestMethod]
    public void ConcordancerOperationsTests_GetSuggestions()
    {
        var file = TestUtility.CreateProcessedFile("This is a simple text file.");

        var suggestions = ConcordanceOperations.GetSuggestions("This", SuggestionType.WordForm);
        Assert.AreEqual(1, suggestions.Count);
        Assert.AreEqual("This", suggestions[0].Content);

        suggestions = ConcordanceOperations.GetSuggestions("text", SuggestionType.WordForm);
        Assert.AreEqual(1, suggestions.Count);
        Assert.AreEqual("text", suggestions[0].Content);

        TestUtility.LemmatizeWordsSameAsOriginalWordWithoutLastCharacter();

        suggestions = ConcordanceOperations.GetSuggestions("Thi", SuggestionType.BaseForm);
        Assert.AreEqual(1, suggestions.Count);
        Assert.AreEqual("Thi", suggestions[0].Content);

        suggestions = ConcordanceOperations.GetSuggestions("tex", SuggestionType.BaseForm);
        Assert.AreEqual(1, suggestions.Count);
        Assert.AreEqual("tex", suggestions[0].Content);
    }

    [TestMethod]
    public void ConcordancerOperationsTests_GetSuggestions_is_case_insensitive()
    {
        var file = TestUtility.CreateProcessedFile("This is a simple text file.");

        var suggestions = ConcordanceOperations.GetSuggestions("t", SuggestionType.WordForm);
        Assert.AreEqual(2, suggestions.Count);
        Assert.AreEqual("This", suggestions[0].Content);
        Assert.AreEqual("text", suggestions[1].Content);
    }
}
}
